<?php

namespace app\common\model\worker;

use app\common\model\AdminModel;
use app\common\model\BaseModel;
use think\facade\Db;


class CustomerPaymentModel extends BaseModel
{
    protected $table = 'customer_payment';


    public function _formatList($list){
        $customerIds = array_column($list,'customer_id');
        $customerList = CustomerModel::where('id','in',$customerIds)->column('*','id');
        $adminList = AdminModel::column('*','id');
        foreach ($list as $k=>$v){
            $v['customer_info'] = $customerList[$v['customer_id']] ?? [];
            $v['action_admin_nickname'] = $adminList[$v['action_admin_id']]['nickname'] ?? '';
            $list[$k] = $v;
        }
        return $list;
    }

    public function add($data){
        $amount = $data['pay_amount']*$data['is_pay'];

        $data['pay_time'] = empty($data['pay_time'])?date('Y-m-d H:i:s'):$data['pay_time'];
        Db::table('customer')->where('id',$data['customer_id'])->inc('payment_amount',$amount)->update();
        $info =  self::create($data);
        return $info;

    }


}
